package com.dhl.cloud.course.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhl.cloud.common.util.DateUtils;
import com.dhl.cloud.common.vdo.CourseBean;
import com.dhl.cloud.course.dao.CourseMapper;
import com.dhl.cloud.course.entity.Course;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CourseService {
	@Autowired
	private CourseMapper courseMapper;

	public List<Course> selectByusername(String username) {
		return courseMapper.selectByusername(username);
	}

	public Course selectByPrimaryKey(Integer id) {
		return courseMapper.selectByPrimaryKey(id);
	}

	public PageInfo<CourseBean> getAllCourseList(String marjor,Integer pageNum,Integer pageSize){
	PageHelper.startPage(pageNum,pageSize);
	List<CourseBean> list =  courseMapper.selectBymarjor(marjor);
	PageInfo<CourseBean> pageInfo = new PageInfo<>(list);
	return pageInfo;
	}

	public int deleteByPrimaryKey(Integer id) {
		return courseMapper.deleteByPrimaryKey(id);
	}

	public int updateCourse(Course c)
	{
		return courseMapper.updateByPrimaryKey(c);
	}

	public int insert(Course c) {
		if (c.getId() != null)
		{
			Course cc = courseMapper.selectByPrimaryKey(c.getId());
			cc.setMarjor(c.getMarjor());
			cc.setIslive(c.getIslive());
			cc.setImgurl(c.getImgurl());
			cc.setName(c.getName());
			cc.setDescrible(c.getDescrible());
			cc.setModifytime(DateUtils.getDateTime());
			return courseMapper.updateByPrimaryKey(cc);
		}
		else
		{
			c.setCreatetime(DateUtils.getDateTime());
			return courseMapper.insert(c);
		}

	}
}
